package com.rykj.energy.basic.rest;

import com.alibaba.fastjson.JSONObject;
import com.rykj.butler.sdk.common.ResponseResult;
import com.rykj.energy.common.utils.Result;
import com.rykj.energy.database.pojo.Organization;
import com.rykj.energy.database.pojo.vo.OrgMemberVo;
import com.rykj.energy.database.pojo.vo.OrganizationVO;
import com.rykj.energy.database.service.IOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "区域管理")
@RestController
@RequestMapping("/manage")
@Slf4j
public class OrganizationController {

    @Autowired
    private IOrganizationService orgService;

    @ApiOperation("区域创建")
    @PostMapping("/org/create")
    public Result createOrg(@RequestBody Organization orgParam){
       try{
           orgService.createOrg(orgParam);
           JSONObject retObj=new JSONObject();
           retObj.put("id",orgParam.getId());
           return Result.success("新建成功",retObj);
       }catch (Exception e){
          log.error("===创建区域异常===",e);
          return  Result.failure("创建区域异常");
       }
    }


    @ApiOperation("区域查看")
    @GetMapping("/org/findByOrgId/{id}")
    @ApiImplicitParam(name = "id",value = "区域id",required = true,paramType = "path")
    public Result<Organization> findByOrgId(@PathVariable("id") Integer id){
        try{
            Organization org = orgService.findByOrgId(id);
            return Result.success("查询成功",org);
        }catch (Exception e){
            log.error("===查询区域异常===",e);
            return  Result.failure("查询区域异常");
        }
    }


    @ApiOperation("区域修改")
    @PostMapping("/org/update")
    public Result updateOrg(@RequestBody Organization orgParam){
        try{
            orgService.updateOrg(orgParam);
            JSONObject retObj=new JSONObject();
            retObj.put("id",orgParam.getId());
            return Result.success("修改成功",retObj);
        }catch (Exception e){
            log.error("===修改区域异常===",e);
            return  Result.failure("修改区域异常");
        }
    }

    @ApiOperation("区域删除")
    @GetMapping("/org/delete")
    @ApiImplicitParam(name = "id",value = "区域id",required = true,paramType = "query")
    public Result deleteByOrgId( @RequestParam(value = "id") Integer id){
        try{
             orgService.deleteByOrgId(id);
            return Result.success("删除成功",null);
        }catch (Exception e){
            log.error("===删除区域异常===",e);
            return  Result.failure("删除区域异常");
        }
    }


    @ApiOperation("区域树查询")
    @GetMapping("/org/get_org_tree")
    public Result<List<OrganizationVO>> getOrgTree( ){
        try{
            List<OrganizationVO>  orgTreeVos =orgService.getOrgTree(0,null);
            return Result.success(orgTreeVos);
        }catch (Exception e){
            log.error("===区域树查询区域异常===",e);
            return  Result.failure("区域树查询区域异常");
        }
    }

    @ApiOperation("区域下的列表查询")
    @GetMapping("/org/getListByOrgId")
    public Result<List<OrgMemberVo>> getListByOrgId(@RequestParam(value = "keyWord", required = false) String keyWord,
                                     @RequestParam(value = "orgId", required = false) Integer orgId) {
        try{
            List<OrgMemberVo>  orgTreeVos =orgService.getListByOrgId(keyWord,orgId);
            return Result.success(orgTreeVos);
        }catch (Exception e){
            log.error("===区域下的列表查询异常===",e);
            return  Result.failure("区域下的列表查询异常");
        }
    }
}
